package com.czy.owner.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.czy.owner.R;

/**
 * Created by King on 2016/7/11 0011.
 */
public class DialogCall {
    private Dialog mDialog;
    private Display mDisplay;
    private View dialogView;
    private TextView tvMsg;
    private Button btnSure;

    public DialogCall(Context context, int themeResId) {
        if (mDisplay == null) {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            mDisplay = windowManager.getDefaultDisplay();

        }
        if (mDialog == null) {
            dialogView = LayoutInflater.from(context).inflate(themeResId, null);
            tvMsg = (TextView) dialogView.findViewById(R.id.tv_msg);
            ((Button) dialogView.findViewById(R.id.btn_cancel)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialog.dismiss();
                }
            });
            btnSure = (Button) dialogView.findViewById(R.id.btn_sure);
            mDialog = new Dialog(context, R.style.AlertDialogStyle);
            mDialog.setContentView(dialogView);
            mDialog.setCancelable(true);
            mDialog.setCanceledOnTouchOutside(true);

            Window dialogWindow = mDialog.getWindow();
            WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
            //        p.height = (int) (d.getHeight() * 0.6);
            p.width = (int) (mDisplay.getWidth() * 0.8);
            dialogWindow.setAttributes(p);
        }
        if (mDialog != null) {
            mDialog.dismiss();
        }

    }

    public DialogCall dismiss() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        return this;
    }

    public DialogCall setContent(String content) {
        dismiss();
        if (tvMsg != null) {
            tvMsg.setText(content);
        }
        return this;
    }

    public DialogCall setListener(View.OnClickListener l) {
        if (btnSure != null) {
            btnSure.setOnClickListener(l);
        }
        return this;
    }

    public DialogCall show() {
        if (mDialog != null) {
            mDialog.show();
        }
        return this;
    }
}
